package com.example.smartcity1.my;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.smartcity1.R;
import com.example.smartcity1.bean.MsgCodeBean;
import com.example.smartcity1.bean.MyRegisterPostBean;
import com.example.smartcity1.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyRegisterActivity extends AppCompatActivity {
    EditText editUserName;
    EditText editNickName;
    EditText editPhoneNumber;
    EditText editPassword;
    RadioGroup radioGroup;
    RadioButton radioButton1;
    RadioButton radioButton0;
    Button button;
    private String sex = "1";

    private static final String TAG = "MyRegister";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_register);
        getSupportActionBar().setTitle("注册");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initView();
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.my_register_sex_1:
                    sex = "1";
                    break;
                case R.id.my_register_sex_0:
                    sex = "0";
                    break;
            }
        });
        button.setOnClickListener(v -> {
            String userName = editUserName.getText().toString();
            String nickName = editNickName.getText().toString();
            String phoneNumber = editPhoneNumber.getText().toString();
            String password = editPassword.getText().toString();
            MyRegisterPostBean myRegisterPostBean = new MyRegisterPostBean(userName,nickName,phoneNumber,sex,password);
            RetrofitClient.appService().getMyRegisterData(myRegisterPostBean).enqueue(new Callback<MsgCodeBean>() {
                @Override
                public void onResponse(Call<MsgCodeBean> call, Response<MsgCodeBean> response) {
                    MsgCodeBean msgCodeBean = response.body();
                    if (msgCodeBean != null) {
                        if (msgCodeBean.getCode() == 200) {
                            Toast.makeText(MyRegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor editor = getSharedPreferences("user",MODE_PRIVATE).edit();
                            editor.putString("username",userName);
                            editor.putString("password",password);
                            editor.apply();
                            finish();
                        } else if (msgCodeBean.getCode() == 500) {
                            Toast.makeText(MyRegisterActivity.this, msgCodeBean.getMsg(), Toast.LENGTH_SHORT).show();
                            Log.d(TAG, "onResponse: "+ msgCodeBean.getMsg());
                        }
                    }
                }
                @Override
                public void onFailure(Call<MsgCodeBean> call, Throwable t) {

                }
            });
        });
    }

    private void initView() {
        editUserName = findViewById(R.id.my_register_user_name);
        editNickName = findViewById(R.id.my_register_nick_name);
        editPhoneNumber = findViewById(R.id.my_register_phone_number);
        editPassword = findViewById(R.id.my_register_password);
        radioGroup = findViewById(R.id.my_register_sex);
        radioButton1 = findViewById(R.id.my_register_sex_1);
        radioButton0 = findViewById(R.id.my_register_sex_0);
        button = findViewById(R.id.my_register_register);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }
}